---
title: "Figure 3"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Note

The code in "ecolRxC_accuracy_Thomsen_probit_no_aprox.html" must be previously run for this code to run. The packages **tidyverse** and **gridExtra** are needed for this code to run.


## Introduction

This document contains the reproducible code to produce Figure 3.

```{r}
# Figure 3
library(tidyverse)
EI.AV <- read.csv("summary_ecolRxC_solutions_Thomsen_probit_FALSE.csv", 
                  sep =";", stringsAsFactors = F)
EI.AV <- EI.AV[, c("year", "district", "EI.ecolRxC.AVCR", "EPW.ecolRxC.AVCR")]
names(EI.AV)[c(3, 4)] <- c("EI.AV", "EPW.AV") 
EI.AV <- EI.AV[order(EI.AV$EI.AV), ]
EI.AV$orden1 <- 1:nrow(EI.AV)
EI.AV <- EI.AV[order(EI.AV$EPW.AV), ]
EI.AV$orden2 <- 1:nrow(EI.AV)

EI.Ref <- read.csv("pjk_ecolRxC_predictions_Thomsen_probit_FALSE.csv", 
                   sep =";", stringsAsFactors = F) 
EI.Ref <- EI.Ref[, c("year", "district", "EI.ecolRxC.refjk", "EPW.ecolRxC.refjk")]
names(EI.Ref)[c(3, 4)] <- c("EI.Ref", "EPW.Ref")
EI.AV$ID <- paste(EI.AV$distric, EI.AV$year, sep =".")
EI.Ref$ID <- paste(EI.Ref$distric, EI.Ref$year, sep =".")

EI.AV <- EI.AV[, c("ID", "EI.AV", "EPW.AV", "orden1", "orden2")]
EI.Ref <- EI.Ref[, c("ID", "EI.Ref", "EPW.Ref")]

EI.Ref <- merge(EI.Ref, EI.AV)

p1 <- ggplot(EI.Ref, aes(x = orden1, y = EI.Ref)) +
  geom_point(color = "black", alpha = 0.15, size = 0.35, shape = 16) +
  geom_point(aes(y = EI.AV), color = "red", size = 0.50, shape = 16) +
  ggplot2::theme_bw() +
  ggplot2::theme(
    panel.background = ggplot2::element_blank(),
    legend.title = element_blank(), 
    legend.position="bottom",
    axis.text.x = ggplot2::element_blank(),
    axis.ticks.x = ggplot2::element_blank(),
    #          legend.spacing.x = unit(0.1, 'cm'),
    strip.background = element_rect(fill = "white"),
    legend.margin = margin(c(0, 2, 2, 0)),
    legend.text = element_text(margin = margin(r = 20, unit = "pt"), size = 11)
  ) +
  xlab("Election/District") + ylab("EI: error index") 

p2 <- ggplot(EI.Ref, aes(x = orden1, y = EPW.Ref)) +
  geom_point(color = "black", alpha = 0.15, size = 0.35, shape = 16) +
  geom_point(aes(y = EPW.AV), color = "red", size = 0.50, shape = 16) +
  ggplot2::theme_bw() +
  ggplot2::theme(
    panel.background = ggplot2::element_blank(),
    legend.title = element_blank(), 
    legend.position="bottom",
    axis.text.x = ggplot2::element_blank(),
    axis.ticks.x = ggplot2::element_blank(),
    #          legend.spacing.x = unit(0.1, 'cm'),
    strip.background = element_rect(fill = "white"),
    legend.margin = margin(c(0, 2, 2, 0)),
    legend.text = element_text(margin = margin(r = 20, unit = "pt"), size = 11)
  ) +
  xlab("Election/District") + ylab("EPW: discrepancy index") 

library(gridExtra)
grid.arrange(p1, p2, ncol=2)

tiff("Figure3.tiff", height = 3, width = 9, units = 'in', compression = "lzw", res = 300)
   grid.arrange(p1, p2, ncol=2)
dev.off()


```
